package net.es.lookup.scalability.testing;

import org.ho.yaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: luke
 * Date: 8/31/13
 * Time: 1:21 PM
 * To change this template use File | Settings | File Templates.
 */
public class Configuration {

    public static HashMap<String, Object> addresses;
    public static HashMap<String, Object> userMap;
    public static HashMap<String, Object> clients;

    public static List<List<String>> clientHosts;
    public static String clientProgram;
    public static String workingDir;
    public static String resultRepo;
    public static String slsServer;
    public static int port;

    public static String username;
    public static String sshKeyFile;

    public static String test;
    public static String clientDist;
    public static List<String> distPattern;

    public static int totalClients;
    public static int maxPerHost;
    public static int minPerHost;
    public static int pause;

    public static void configure(String configFile) {

        HashMap<String, Object> configMap = (HashMap) getConfiguration(configFile);

        addresses = (HashMap<String, Object>) configMap.get("addresses");
        userMap = (HashMap<String, Object>) configMap.get("user");
        clients = (HashMap<String, Object>) configMap.get("clients");

        clientHosts = (List<List<String>>) addresses.get("client-hosts");
        clientProgram = (String) addresses.get("client-program");
        workingDir = (String) addresses.get("host-working-dir");
        resultRepo = (String) addresses.get("result-repo");
        slsServer = (String) addresses.get("sls-server-host");
        port = (Integer) addresses.get("sls-server-port");

        username = (String) userMap.get("username");
        sshKeyFile = (String) userMap.get("ssh-key-file");

        test = (String) clients.get("test");
        clientDist = (String) clients.get("distribution");
        distPattern = null;
        if (!clientDist.equals("unspecified")) {

            distPattern = (List<String>) clients.get("pattern");
        }
        totalClients = (Integer) clients.get("total-clients");
        maxPerHost = (Integer) clients.get("max-per-host");
        minPerHost = (Integer) clients.get("min-per-host");
        pause = (Integer) clients.get("pause");
    }

    private static Map getConfiguration(String configFile) {

        Map configuration;
        InputStream yamlFile = null;

        try {

            yamlFile = new FileInputStream(new File(configFile));
        } catch (FileNotFoundException e) {

            System.out.println(configFile + " not found\n. Config file required to start tests");
            org.junit.Assert.fail(configFile + " not found\n. Config file required to start tests");
        }

        configuration = (Map) Yaml.load(yamlFile);

        return configuration;
    }
}
